達(dá)夢(mèng)數(shù)據(jù)庫(kù)dm8與PYthon連接訪(fǎng)問(wèn)
發(fā)布時(shí)間:2024/9/12 14:27:42
達(dá)夢(mèng)數(shù)據(jù)庫(kù)
===================
主機(jī)名LOCALHOST
賬號(hào)名
SYSDBA
登錄密碼
123456789
數(shù)據(jù)庫(kù)名DAMENG
實(shí)例名DMSERVER
端口5236
=====================
Pycharm默認(rèn)沒(méi)有連接達(dá)夢(mèng)的驅(qū)動(dòng),首先選擇給Pycharm添加一個(gè)達(dá)夢(mèng)的驅(qū)動(dòng)。
找到自己電腦上達(dá)夢(mèng)的JDBC驅(qū)動(dòng),因?yàn)槲译娔X上安裝了達(dá)夢(mèng)數(shù)據(jù)庫(kù),所有的驅(qū)動(dòng)文件都在他的安裝目錄里。
通過(guò)驅(qū)動(dòng)方式選擇創(chuàng)建數(shù)據(jù)庫(kù)連接。
SYSDBA
123456789
jdbc:dm://127.0.0.1:5236?schema=PRODUCTION
//====python 配置文件===================================
import dmPython
conn = dmPython.connect(user='SYSDBA', password='123456789',
server='localhost', port=5236)
//======python 調(diào)用頁(yè)面=================================
#!/usr/bin/python
# coding:utf-8
import dmPython
from apps.damengDB import conn
import json
try:
cursor = conn.cursor()
print('python: conn success!')
try:
# 清空表,初始化測(cè)試環(huán)境
cursor.execute('delete from PRODUCTION.PRODUCT_CATEGORY')
print('python: delete success!')
except (dmPython.Error, Exception) as err:
print(err)
try:
# 插入數(shù)據(jù)
cursor.execute("insert into PRODUCTION.PRODUCT_CATEGORY(NAME) values('88888'), ('66666')")
print('python: insert success!')
# 刪除數(shù)據(jù)
# cursor.execute("delete from PRODUCTION.PRODUCT_CATEGORY where name='Go'")
# print('python: delete success!')
#
# # 更新數(shù)據(jù)
# cursor.execute('update PRODUCTION.PRODUCT_CATEGORY set name = \'C++\' where name=\'C\'')
# print('python: update success!')
#
# # 查詢(xún)數(shù)據(jù)
# cursor.execute("select name from PRODUCTION.PRODUCT_CATEGORY")
cursor.execute("call mytest_proc(parm_OPType=>101);")
rows = cursor.fetchall()
# 列名
columns = [column[0] for column in cursor.description]
# 將獲取的數(shù)據(jù)轉(zhuǎn)換為字典列表
results = []
for row in rows:
result = dict(zip(columns, row))
results.append(result)
print("字典")
print(results)
for tmp in results:
print(tmp["NAME"])
# 將字典列表轉(zhuǎn)換為JSON格式的字符串
json_data = json.dumps(results)
# 輸出JSON數(shù)據(jù)
print("輸出JSON數(shù)據(jù)")
print(json_data)
# for tmp in json_data:
# print(tmp)
# =======================================
# # 將結(jié)果轉(zhuǎn)換為列表的字典
# results = []
# for row in rows:
# results.append(dict(zip(cursor.description, row)))
#
# # 將列表轉(zhuǎn)換為JSON字符串
# json_data = json.dumps(results)
#
# # 輸出JSON字符串
# print(json_data)
# == == == == == == == == == == == == == == == == == == == =
# for tmp in res:
# print(tmp)
# print('-----------')
# print(tmp[0])
# print(tmp[1])
# # for c1 in tmp:
# # print(c1)
print('python: select success!')
except (dmPython.Error, Exception) as err:
print(err)
conn.close()
except (dmPython.Error, Exception) as err:
print(err)
//=================================
pip install Flask Flask-SQLAlchemy pymssql